package charts;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import org.jCharts.axisChart.AxisChart;
import org.jCharts.chartData.ChartDataException;
import org.jCharts.chartData.DataSeries;
import org.jCharts.chartData.StockChartDataSet;
import org.jCharts.properties.AxisProperties;
import org.jCharts.properties.ChartProperties;
import org.jCharts.properties.DataAxisProperties;
import org.jCharts.properties.LegendProperties;
import org.jCharts.properties.PropertyException;
import org.jCharts.properties.StockChartProperties;

import businessEntities.Cours;
import businessEntities.DateCours;

public class StockChart {
	public static AxisChart getChart(String titreChart, String xAxisTitle, String yAxisTitle, ArrayList<Cours> lesCours, ArrayList<Date> lDate, int sizeLargeur, int sizeHauteur) throws ChartDataException, PropertyException{
		
		int taille = 0;
		if(lDate!=null)taille=lDate.size();
		double[] highs= new double[taille];
		double[] lows= new double[taille];
		double[] opens= new double[taille];
		double[] closes= new double[taille];
		String[] xAxisLabels = new String[taille];
		
		int amplifi = 1;
		for(int i=0;i<taille;i++){
			Date dTemp = lDate.get(i);
			highs[i]=lesCours.get(i).getHaut()*amplifi;
			lows[i]=lesCours.get(i).getBas()*amplifi;
			opens[i]=lesCours.get(i).getOuverture()*amplifi;
			closes[i]=lesCours.get(i).getFermeture()*amplifi;
			xAxisLabels[i] = DateCours.getDate(dTemp, DateCours.FORMAT_DD_MM_YYYY);
		}
		
		DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, titreChart );
		
		StockChartProperties stockChartProperties= new StockChartProperties();
		
		StockChartDataSet stockChartDataSet;
		stockChartDataSet= new StockChartDataSet( highs, "High", lows, "Low", Color.black, stockChartProperties );
		stockChartDataSet.setOpenValues( opens, "Open", Color.green );
		stockChartDataSet.setCloseValues( closes, "Close", Color.red );

		dataSeries.addIAxisPlotDataSet( stockChartDataSet );

		ChartProperties chartProperties= new ChartProperties();
		AxisProperties axisProperties;
		
		axisProperties= new AxisProperties();
		
		axisProperties.setXAxisLabelsAreVertical(false);
		LegendProperties legendProperties= new LegendProperties();

		AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, sizeLargeur, sizeHauteur );
		
		return axisChart;
	}
}
